package com.zj.study;

import java.util.ArrayDeque;
import java.util.Deque;

public class cachedao计算器 {

    public static void main(String[] args) {
        String str1 = "(add (sub 15 (add 1 3)) (mul (add 13 3) 4))";
        System.out.println(getRes(str1));

    }

    public static Integer getRes(String str1) {
        Deque<String> optQue = new ArrayDeque<>();
        Deque<Integer> nums = new ArrayDeque<>();
        int num = 0;
        int start = 0;
        for (int i = 0; i < str1.length(); i++) {
            if (str1.charAt(i) == '(') {
                start = i;
            } else if (str1.charAt(i) == ')') {
                switch (optQue.pop()) {
                    case "(add":
                        num = nums.pop() + num;
                        break;
                    case "(sub":
                        num = nums.pop() - num;
                        break;
                    case "(mul":
                        num = nums.pop() * num;
                        break;
                    case "(div":
                        num = num == 0 ? null : nums.pop() / num;
                        break;
                }
                if (i != str1.length() - 1 && str1.charAt(i + 1) == ' ') {
                    nums.push(num);
                    num = 0;
                }
            } else if (Character.isLetter(str1.charAt(i)) && str1.charAt(i + 1) == ' ') {
                optQue.push(str1.substring(start, i + 1));
            } else if (Character.isDigit(str1.charAt(i))) {
                num = str1.charAt(i) - '0' + 10 * num;
                if (str1.charAt(i + 1) == ' ') {
                    nums.push(num);
                    num = 0;
                }
            }
        }
        return num;
    }

    public static Integer getRes2(String str1) {
        String[] split = str1.split(" ");
        Deque<String> opt = new ArrayDeque<>();
        Deque<String> nums = new ArrayDeque<>();
        for (String str : split) {
            if (str.endsWith("add") || str.endsWith("sub") || str.endsWith("mul") || str.endsWith("div")) {
                opt.push(str);
            } else {
                while (str.endsWith(")")) {
                    String nums2 = str.substring(0, str.indexOf(")"));
                    int res = compute(opt.pop(), Integer.parseInt(nums.pop()), Integer.parseInt(nums2));
                    String sufix = str.substring(str.indexOf(")") + 1);
                    if (sufix.length() > 0) {
                        str = res + sufix;
                    } else {
                        str = String.valueOf(res);
                    }
                }
                nums.push(str);

            }
        }
        return Integer.parseInt(nums.pop());
    }

    public static Integer compute(String opt, Integer num1, Integer num2) {
        switch (opt) {
            case "(add":
                return num1 + num2;
            case "(sub":
                return num1 - num2;
            case "(mul":
                return num1 * num2;
            case "(div":
                if (num2 == 0) {
                    return null;
                }
                return num1 / num2;
        }
        return null;
    }
}
